package biz.inspeed.test.queu;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.ServerSocketChannel;
import java.util.concurrent.BlockingQueue;

public class Server implements Runnable {

	
	public static void main(String[] args){
		new Thread(new Server()).start();
	}
	@Override
	public void run() {
		while(true){
			try {
				Socket socket = serverSocket.accept();
				System.out.println("receive " + socket.getRemoteSocketAddress());
				InputStream in = socket.getInputStream();
				OutputStream out = socket.getOutputStream();
				byte[] buffer = new byte[1024];
				int len = 0;
				int time = 0;
				while((len = in.read(buffer)) != -1){
					out.write(buffer, 0, len);
					System.out.println(time ++  + "\t"+ "buffe " + buffer.toString()  + "--"  + len);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private BlockingQueue<Object> sSendQueue;
	private BlockingQueue<Object> sReceiveQueue;
	
	public Server(){
		this.sReceiveQueue = DataStore.sReceiveQueue;
		this.sSendQueue = DataStore.sSendQueue;
		init();
	}
	
	private ServerSocket serverSocket;
	private int port = 0xBeef;
	private void init(){
		try {
			this.serverSocket = new ServerSocket(port);
			System.out.println("Server start up... ");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
